package top.saodisheng.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.saodisheng.pojo.User;
import top.saodisheng.service.UserService;
import top.saodisheng.util.Page4Navigator;

/**
 * Description:
 *
 * @author 扫地生_saodisheng
 */
@RestController
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/users")
    public Page4Navigator<User> list(@RequestParam(value = "start", defaultValue = "0") int start,
                                     @RequestParam(value = "size", defaultValue = "5") int size) {
        start = start < 0 ? 0 : start;
        Page4Navigator<User> page = userService.list(start, size, 5);
//        for (User u : page.getContent()) System.out.println(u);
        return page;
    }

    // 获取一个用户
    @GetMapping("/users/{id}")
    public  User get(@PathVariable("id") int id) {
        return userService.get(id);
    }

    // 删除一个用户
    @DeleteMapping("/users/{id}")
    public String delete(@PathVariable("id") int id) {
        userService.delete(id);
        return null;
    }

    // 添加一个用户
    @PostMapping("/users")
    public Object add(@RequestBody User bean) {
        userService.add(bean);
        return bean;
    }

    // 更新用户
    @PutMapping("/users")
    public Object update(@RequestBody User bean) {
        userService.update(bean);
        return bean;
    }


}
